﻿@page "/admin/groups"

@inject IHttpService _httpService
@inject IPopupService PopupService
@inject IRepository<RobotGroup> _robotGroupRepository

<DataTableCard TModel="RobotGroup" Headers="_headers" DataSource="_robotGroupRepository.GetAll()" Title="@($"{_name}列表")" OnClickAdd="AddItem" @ref="dataTableCard"
                Predicate="s => dataTableCard==null ||string.IsNullOrWhiteSpace(dataTableCard._search) || (s.Note.Contains(dataTableCard._search))">
    <ItemColContent>
        @if (context.Header.Value == "actions")
        {
            <MIcon Color="success"
                   Class="mr-2"
                   OnClick="()=>EditItem(context.Item)">mdi-pencil</MIcon>
        }
        else if (context.Header.Value == nameof(RobotGroup.IsHidden))
        {
            <CnGalWebSite.RobotClientX.Components.Replies.EnableChip Enabled="!context.Item.IsHidden" />
        }
        else if (context.Header.Value == nameof(RobotGroup.ForceMatch))
        {
            <CnGalWebSite.RobotClientX.Components.Groups.MatchChip ForceMatch="context.Item.ForceMatch" />
        }
        else
        {
            @context.Value
        }
    </ItemColContent>
</DataTableCard>

<MDialog @bind-Value="_editDialog"
         MaxWidth="1200">
    <CnGalWebSite.RobotClientX.Components.Groups.EditCard Id="_editedItemId" AllowedScopes="_scopes" OnEdited="OnEdited" @ref="editCard" />
</MDialog>

@code {
    private bool _editDialog;
    private long _editedItemId;
    private bool _detailDialog;

    private string _name = "QQ群";

    CnGalWebSite.RobotClientX.Components.Groups.EditCard editCard;

    private List<DataTableHeader<RobotGroup>> _headers = new List<DataTableHeader<RobotGroup>>
{
          new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(RobotGroup.Id)
          },
            new (){ Text= "备注", Value= nameof(RobotGroup.Note)},
            new (){ Text= "群号", Value= nameof(RobotGroup.GroupId)},
            new (){ Text= "匹配模式", Value= nameof(RobotGroup.ForceMatch)},
            new (){ Text= "状态", Value= nameof(RobotGroup.IsHidden)},
            new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    List<KeyValuePair<string, string>> _scopes;

    DataTableCard<RobotGroup> dataTableCard;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    public void EditItem(RobotGroup item)
    {
        _editedItemId = item.Id;
        _editDialog = true;
        StateHasChanged();
        if (editCard != null)
        {
            editCard.Refresh();
        }
    }

    public void OnEdited(RobotGroup model)
    {
        _editDialog = false;
        StateHasChanged();
        if (dataTableCard != null)
        {
            dataTableCard.GetDataFromApi();
        }
    }

    public void AddItem()
    {
        _editedItemId = 0;
        _editDialog = true;
        StateHasChanged();
        if (editCard != null)
        {
            editCard.Refresh();
        }
    }

}
